/*
    This is a set of libraries to assist in OpenGl and Other Applications
    Copyright (C) 2008  The Modular Engine Team

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TIMERMODULE_H
#define TIMERMODULE_H
#include <time.h>
#include <vector>
#include "EngineCore.h"

#undef CLASSDECL
#ifdef TIMERMODULE
	#define CLASSDECL __declspec(dllexport)
#else
	#pragma comment( lib, "TimerModule.lib" )
	#define CLASSDECL __declspec(dllimport)
#endif

namespace ModularEngine
{
	/// A simple timer to find duration since something
	class CLASSDECL Timer
	{
	protected:
		time_t mtStartTime;
	public:
		Timer()
		{
			mtStartTime = clock();
		}
		virtual void update();
		void reset();
		time_t timeElapsed();
	};

	/// Executes a functor everytime mPeriod milliseconds have passed
	class CLASSDECL PeriodicExecutor: public Timer
	{
	protected:
		/// Time between executions in milliseconds
		unsigned int mPeriod;
		/// Functor to execute
		Functor *mpFunctor;
	public:
		/// Updates time and if time exceeds mPeriod execute the functor
		void update();
		/// Setup function for this class.
		void startTimer( unsigned int uPeriod, Functor *pFunctor );

	};

	class CLASSDECL ExecuteTimer: public PeriodicExecutor
	{
	protected:
		bool mbExecuted;
	public:		
		ExecuteTimer()
		{
			mbExecuted = false;
		}
		void update();
		void reset();
	};

	/// Timer module which mainly controls the Executor timers

	/// If this is never run by the core engine, none of the periodic
	/// executors will work.
	class CLASSDECL TimerModule: BASE(TimerModule)
	{
	protected:
		/// Vector containing all the timers.
		std::vector<Timer *> mvTimers;

	public:
		/// add a Timer to the stack
		/// only useful for one-timers and periodic executioners
		void addTimer( Timer *pTimer );

		int initialize();
		int run();

	};
}

#endif
